package com.cn.kehong.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.ILaborRateService;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * 劳率controller
 */
@Controller
@RequestMapping("/labRate")
public class LaborRateController {

    @Autowired
    private ILaborRateService labRateService;

    @RequestMapping("/getLaborRateLists/{frontToken}")
    @ResponseBody
    public JsonResponse<List<LaborRate>> getLaborRateLists(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<LaborRate> > response = new JsonResponse<List<LaborRate> >();
        List<LaborRate> lists = labRateService.getLaborRateLists(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/getLaborRateListsByTimeAndFactoryName/{frontToken}")
    @ResponseBody
    public JsonResponse<List<LaborRate>> getLaborRateListsByTimeAndFactoryName(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<LaborRate> > response = new JsonResponse<List<LaborRate> >();
        List<LaborRate> lists = labRateService.getLaborRateListsByTimeAndFactoryName(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }


    /**
     * 劳率新增接口
     */
    @RequestMapping("/addLaborRate/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addLaborRate(@RequestBody LaborRate laborRate, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = labRateService.addLaborRate(laborRate);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/validateLaborRate/{frontToken}")
    @ResponseBody
    public JsonResponse<String> validateLaborRate(QueryForm queryForm, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = labRateService.validateLaborRate(queryForm);
        return CommonUtil.getResponseData(response,result);
    }

    /**
     *保存按钮的接口
     */
    @RequestMapping("/updateLaborRate/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateLaborRate(@RequestBody LaborRate laborRate, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = labRateService.updateLaborRate(laborRate);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/deleteLaborRateById/{frontToken}")
    @ResponseBody
    public JsonResponse<String> deleteLaborRateById(String id,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = labRateService.deleteLaborRateById(id);
        return CommonUtil.getResponseData(response,result);
    }


    //劳率下载接口
    @ResponseBody
    @RequestMapping("/exportLaborRate")
    public void exportLaborRate(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setLaborTime(jsonObject.getString("laborTime"));
            queryForm.setFactoryWorkshop(jsonObject.getString("factoryName"));
        }
        labRateService.exportLaborRate(queryForm,response);
    }

    //劳率分析达成率，生产天数。。。
    @RequestMapping("/getListByRateAnalysis/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getListByRateAnalysis(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  listByRateAnalysis = labRateService.getListByRateAnalysis(queryForm);
        response.setResult(listByRateAnalysis);
        response.setSuccess(true);
        return response;
    }
}
